package org.jdemo.search.lucene;

import com.cyou.fz.testdata.generator.RandomUtil;
import com.cyou.fz.testdata.generator.WordUtil;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class WriteDemo {

    public static void main(String[] args) throws Exception {
        long startTime = System.currentTimeMillis();

        // 配置索引
        Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_47);
        Directory directory = FSDirectory.open(new File("F:\\temp\\luceneidx"));
        IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_47, analyzer);
        final IndexWriter writer = new IndexWriter(directory, config);

        try {
            ExecutorService executorService = Executors.newFixedThreadPool(200);
            List<Future<Boolean>> futures = new ArrayList<Future<Boolean>>();
            for (int i = 0; i < 1000; i++) {
                Future<Boolean> future = executorService.submit(new Callable<Boolean>() {
                    @Override
                    public Boolean call() throws Exception {
                        List<Document> docs = new ArrayList<Document>();
                        for (int j = 0; j < 1000; j++) {
                            Document doc = new Document();
                            doc.add(new Field("id", RandomUtil.randomNum(10) + "", TextField.TYPE_STORED));
                            doc.add(new Field("title", WordUtil.randomZHContent(10, 20, false), TextField.TYPE_STORED));
                            doc.add(new Field("subtitle", WordUtil.randomZHContent(5, 10, false), TextField.TYPE_STORED));
                            doc.add(new Field("fullTitle", WordUtil.randomZHContent(10, 30, false), TextField.TYPE_STORED));
                            doc.add(new Field("link", WordUtil.randomENContent(10, 50, false), TextField.TYPE_STORED));
                            doc.add(new Field("newWindow", WordUtil.randomENContent(10, 50, false), TextField.TYPE_STORED));
                            doc.add(new Field("color", "#" + RandomUtil.randomLetter(6), TextField.TYPE_STORED));
                            doc.add(new Field("metaDescription", WordUtil.randomENContent(50, 500, true), TextField.TYPE_STORED));
                            doc.add(new Field("infoPath", WordUtil.randomENContent(10, 50, false), TextField.TYPE_STORED));
                            doc.add(new Field("infoTemplate", WordUtil.randomENContent(10, 50, false), TextField.TYPE_STORED));
                            doc.add(new Field("source", WordUtil.randomENContent(10, 50, false), TextField.TYPE_STORED));
                            doc.add(new Field("sourceUrl", WordUtil.randomENContent(10, 50, false), TextField.TYPE_STORED));
                            doc.add(new Field("author", WordUtil.randomZHWord(), TextField.TYPE_STORED));
                            doc.add(new Field("smallImage", WordUtil.randomENContent(10, 50, false), TextField.TYPE_STORED));
                            doc.add(new Field("largeImage", WordUtil.randomENContent(10, 50, false), TextField.TYPE_STORED));
                            doc.add(new Field("video", WordUtil.randomENContent(10, 50, false), TextField.TYPE_STORED));
                            doc.add(new Field("stepName", WordUtil.randomENWord(), TextField.TYPE_STORED));
                            doc.add(new Field("allowComment", "false", TextField.TYPE_STORED));
                            docs.add(doc);
                        }
                        try {
                            writer.addDocuments(docs);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        return true;
                    }
                });
                futures.add(future);
            }
            int i = 0;
            for (Future<Boolean> future : futures) {
                future.get();
                System.out.println(i++);
            }
            writer.commit();
            System.out.println("耗时: " + (System.currentTimeMillis() - startTime) + "ms");
        } finally {
            writer.close();
            directory.close();
        }
    }
}
